
--region [dbo].[AddPatientRecordTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddPatientRecordTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddPatientRecordTrg' AND type = 'TR')
   DROP TRIGGER AddPatientRecordTrg
GO

CREATE TRIGGER AddPatientRecordTrg
ON [PatientRecord]
FOR INSERT 
AS
	UPDATE 
		[Disease]
	SET 
		[PatientCount] = [PatientCount] + 1
	FROM 
		[inserted]
	WHERE
		[Disease].[Id] =  [inserted].[DiseaseId]
GO

--endregion


--region [dbo].[DeletePatientRecordTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[DeletePatientRecordTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'DeletePatientRecordTrg' AND type = 'TR')
   DROP TRIGGER DeletePatientRecordTrg
GO

CREATE TRIGGER DeletePatientRecordTrg
ON [PatientRecord]
FOR DELETE 
AS
	UPDATE 
		[Disease]
	SET 
		[PatientCount] = [PatientCount] - 1
	FROM 
		[deleted]
	WHERE
		[Disease].[Id] =  [deleted].[DiseaseId]
GO

--endregion


--region [dbo].[UpdatePatientRecordTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[UpdatePatientRecordTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'UpdatePatientRecordTrg' AND type = 'TR')
   DROP TRIGGER UpdatePatientRecordTrg
GO

CREATE TRIGGER UpdatePatientRecordTrg
ON [PatientRecord]
FOR UPDATE 
AS
	IF UPDATE (DiseaseId)
	BEGIN
		UPDATE 
			[Disease]
		SET 
			[PatientCount] = [PatientCount] - 1
		FROM 
			[deleted]
		WHERE
			[Disease].[Id] =  [deleted].[DiseaseId]

		UPDATE 
			[Disease]
		SET 
			[PatientCount] = [PatientCount] + 1
		FROM 
			[inserted]
		WHERE
			[Disease].[Id] =  [inserted].[DiseaseId]
	END
GO

--endregion


--region [dbo].[AddCurrentSymptomTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddCurrentSymptomTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddCurrentSymptomTrg' AND type = 'TR')
   DROP TRIGGER AddCurrentSymptomTrg
GO

CREATE TRIGGER AddCurrentSymptomTrg
ON [PatientCurrentSymptom]
FOR INSERT 
AS
	UPDATE 
		[Symptom]
	SET 
		[PatientCount] = [PatientCount] + 1
	FROM 
		[inserted]
	WHERE
		[inserted].[DegreeValue] > 0 AND [Symptom].[Id] =  [inserted].[SymptomId]
GO

--endregion


--region [dbo].[DeleteCurrentSymptomTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[DeleteCurrentSymptomTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'DeleteCurrentSymptomTrg' AND type = 'TR')
   DROP TRIGGER DeleteCurrentSymptomTrg
GO

CREATE TRIGGER DeleteCurrentSymptomTrg
ON [PatientCurrentSymptom]
FOR DELETE 
AS
	UPDATE 
		[Symptom]
	SET 
		[PatientCount] = [PatientCount] - 1
	FROM 
		[deleted]
	WHERE
		[deleted].[DegreeValue] > 0 AND [Symptom].[Id] =  [deleted].[SymptomId]
GO

--endregion


--region [dbo].[UpdateCurrentSymptomTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[UpdateCurrentSymptomTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'UpdateCurrentSymptomTrg' AND type = 'TR')
   DROP TRIGGER UpdateCurrentSymptomTrg
GO

CREATE TRIGGER UpdateCurrentSymptomTrg
ON [PatientCurrentSymptom]
FOR UPDATE 
AS
	IF UPDATE (DegreeValue)
	BEGIN
		UPDATE 
			[Symptom]
		SET 
			[PatientCount] = [PatientCount] - 1
		FROM 
			[deleted]
		WHERE
			[deleted].[DegreeValue] > 0 AND [Symptom].[Id] =  [deleted].[SymptomId]

		UPDATE 
			[Symptom]
		SET 
			[PatientCount] = [PatientCount] + 1
		FROM 
			[inserted]
		WHERE
			[inserted].[DegreeValue] > 0 AND [Symptom].[Id] =  [inserted].[SymptomId]
	END
GO

--endregion


--region [dbo].[AddCurrentTreatmentTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddCurrentTreatmentTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddCurrentTreatmentTrg' AND type = 'TR')
   DROP TRIGGER AddCurrentTreatmentTrg
GO

CREATE TRIGGER [AddCurrentTreatmentTrg]
ON [PatientCurrentTreatment]
FOR INSERT 
AS
	UPDATE 
		[Treatment]
	SET 
		[PatientCount] = [PatientCount] + 1
	FROM 
		[inserted]
	WHERE
		[inserted].[IsUsing] > 0 AND [Treatment].[Id] =  [inserted].[TreatmentId]
GO

--endregion


--region [dbo].[DeleteCurrentTreatmentTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[DeleteCurrentTreatmentTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'DeleteCurrentTreatmentTrg' AND type = 'TR')
   DROP TRIGGER DeleteCurrentTreatmentTrg
GO

CREATE TRIGGER DeleteCurrentTreatmentTrg
ON [PatientCurrentTreatment]
FOR DELETE 
AS
	UPDATE 
		Treatment
	SET 
		[PatientCount] = [PatientCount] - 1
	FROM 
		[deleted]
	WHERE
		[deleted].[IsUsing] > 0 AND [Treatment].[Id] =  [deleted].[TreatmentId]
GO

--endregion


--region [dbo].[UpdateCurrentTreatmentTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[UpdateCurrentTreatmentTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'UpdateCurrentTreatmentTrg' AND type = 'TR')
   DROP TRIGGER UpdateCurrentTreatmentTrg
GO

CREATE TRIGGER UpdateCurrentTreatmentTrg
ON [PatientCurrentTreatment]
FOR UPDATE 
AS
	IF UPDATE (IsUsing)
	BEGIN
		UPDATE 
			[Treatment]
		SET 
			[PatientCount] = [PatientCount] - 1
		FROM 
			[deleted]
		WHERE
			[deleted].[IsUsing] > 0 AND [Treatment].[Id] =  [deleted].[TreatmentId]

		UPDATE 
			[Treatment]
		SET 
			[PatientCount] = [PatientCount] + 1
		FROM 
			[inserted]
		WHERE
			[inserted].[IsUsing] > 0 AND [Treatment].[Id] =  [inserted].[TreatmentId]
	END
GO

--endregion


--region [dbo].[AddReplyTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddReplyTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddReplyTrg' AND type = 'TR')
   DROP TRIGGER AddReplyTrg
GO

CREATE TRIGGER [AddReplyTrg]
ON [Reply]
FOR INSERT 
AS
	UPDATE 
		[Group]
	SET 
		[ReplyNum] = [ReplyNum] + 1,
		[PostNum] = [PostNum] + 1
	FROM 
		[inserted],[Topic]
	WHERE
		[Topic].[Id] =  [inserted].[TopicId] AND
		[Group].[Id] = [Topic].[GroupId]

	UPDATE 
		[GroupUser] 
	SET
		[GroupUser].[ReplyCount] = [GroupUser].[ReplyCount] + 1,
		[GroupUser].[PostCount] = [GroupUser].[PostCount] + 1
	FROM 
		[inserted], [Topic], [GroupUser]
	WHERE
		[inserted].[TopicId] = [Topic].[Id] AND
		[GroupUser].[GroupId] = [Topic].[GroupId] AND
		[GroupUser].[UserId] = [inserted].[PostUserId]
GO

--endregion


--region [dbo].[DelReplyTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[DelReplyTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'DelReplyTrg' AND type = 'TR')
   DROP TRIGGER DelReplyTrg
GO

CREATE TRIGGER [DelReplyTrg]
ON [Reply]
FOR DELETE 
AS
	UPDATE 
		[Group]
	SET 
		[ReplyNum] = [ReplyNum] - 1,
		[PostNum] = [PostNum] - 1
	FROM 
		[deleted],[Topic]
	WHERE
		[Topic].[Id] =  [deleted].[TopicId] AND
		[Group].[Id] = [Topic].[GroupId]

	UPDATE 
		[GroupUser] 
	SET
		[GroupUser].[ReplyCount] = [GroupUser].[ReplyCount] - 1,
		[GroupUser].[PostCount] = [GroupUser].[PostCount] - 1
	FROM 
		[deleted], [Topic], [GroupUser]
	WHERE
		[deleted].[TopicId] = [Topic].[Id] AND
		[GroupUser].[GroupId] = [Topic].[GroupId] AND
		[GroupUser].[UserId] = [deleted].[PostUserId]
GO

--endregion


--region [dbo].[AddGroupUserTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddGroupUserTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddGroupUserTrg' AND type = 'TR')
   DROP TRIGGER AddGroupUserTrg
GO

CREATE TRIGGER [AddGroupUserTrg]
ON [GroupUser]
FOR INSERT
AS
	UPDATE 
		[Group]
	SET 
		[UserNum] = [UserNum] + 1
	FROM 
		[inserted],[Group]
	WHERE
		[Group].[Id] =  [inserted].[GroupId] AND
		[inserted].[Role] >= 0
GO

--endregion


--region [dbo].[DelGroupUserTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[DelGroupUserTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'DelGroupUserTrg' AND type = 'TR')
   DROP TRIGGER DelGroupUserTrg
GO

CREATE TRIGGER [DelGroupUserTrg]
ON [GroupUser]
FOR DELETE
AS
	UPDATE 
		[Group]
	SET 
		[UserNum] = [UserNum] - 1
	FROM 
		[deleted],[Group]
	WHERE
		[Group].[Id] =  [deleted].[GroupId] AND
		[deleted].[Role] >= 0
GO

--endregion

--region [dbo].[UpdateGroupUserTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[UpdateGroupUserTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'UpdateGroupUserTrg' AND type = 'TR')
   DROP TRIGGER UpdateGroupUserTrg
GO

CREATE TRIGGER [UpdateGroupUserTrg]
ON [GroupUser]
FOR UPDATE
AS
	UPDATE 
		[Group]
	SET 
		[UserNum] = [UserNum] - 1
	FROM 
		[deleted],[Group]
	WHERE
		[Group].[Id] =  [deleted].[GroupId] AND
		[deleted].[Role] >= 0

	UPDATE 
		[Group]
	SET 
		[UserNum] = [UserNum] + 1
	FROM 
		[inserted],[Group]
	WHERE
		[Group].[Id] =  [inserted].[GroupId] AND
		[inserted].[Role] >= 0
GO

--endregion


--region [dbo].[AddTopicTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddTopicTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddTopicTrg' AND type = 'TR')
   DROP TRIGGER AddTopicTrg
GO

CREATE TRIGGER [AddTopicTrg]
ON [Topic]
FOR INSERT
AS
	UPDATE 
		[Group]
	SET 
		[LastTopicId] = [inserted].[Id],
		[LastTopicTitle] = [inserted].[Title],
		[LastPostTime] = [inserted].[PostTime],
		[LastPostUser] = [inserted].[PostUserName],
		[TopicNum] = [TopicNum] + 1,
		[PostNum] = [PostNum] + 1
	FROM 
		[inserted],[Group]
	WHERE
		[Group].[Id] =  [inserted].[GroupId] 
GO

--endregion


--region [dbo].[AddSymptomHistoryTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddSymptomHistoryTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddSymptomHistoryTrg' AND type = 'TR')
   DROP TRIGGER AddSymptomHistoryTrg
GO

CREATE TRIGGER [AddSymptomHistoryTrg]
ON [PatientSymptomHistory]
FOR INSERT
AS
	UPDATE 
		[PatientRecord]
	SET 
		[UpdateDate] = [inserted].[UpdateDate],
		[UpdateTime] = [UpdateTime] + 1
	FROM 
		[inserted],[PatientRecord]
	WHERE
		[PatientRecord].[Id] =  [inserted].[PRId] 	
GO

--endregion


--region [dbo].[AddTreatmentHistoryTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddTreatmentHistoryTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddTreatmentHistoryTrg' AND type = 'TR')
   DROP TRIGGER AddTreatmentHistoryTrg
GO

CREATE TRIGGER [AddTreatmentHistoryTrg]
ON [PatientTreatmentHistory]
FOR INSERT
AS
	UPDATE 
		[PatientRecord]
	SET 
		[UpdateDate] = [inserted].[UpdateDate],
		[UpdateTime] = [UpdateTime] + 1
	FROM 
		[inserted],[PatientRecord]
	WHERE
		[PatientRecord].[Id] =  [inserted].[PRId] 	
GO

--endregion


--region [dbo].[AddWeightHistoryTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddWeightHistoryTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddWeightHistoryTrg' AND type = 'TR')
   DROP TRIGGER AddWeightHistoryTrg
GO

CREATE TRIGGER [AddWeightHistoryTrg]
ON [PatientWeightHistory]
FOR INSERT
AS
	UPDATE 
		[PatientRecord]
	SET 
		[UpdateDate] = [inserted].[UpdateDate],
		[UpdateTime] = [UpdateTime] + 1
	FROM 
		[inserted],[PatientRecord]
	WHERE
		[PatientRecord].[Id] =  [inserted].[PRId] 	
GO

--endregion


--region [dbo].[AddIndicatorHistoryTrg]

------------------------------------------------------------------------------------------------------------------------
-- Trigger Name: [dbo].[AddIndicatorHistoryTrg]
------------------------------------------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'AddIndicatorHistoryTrg' AND type = 'TR')
   DROP TRIGGER AddIndicatorHistoryTrg
GO

CREATE TRIGGER [AddIndicatorHistoryTrg]
ON [PatientIndicatorHistory]
FOR INSERT
AS
	UPDATE 
		[PatientRecord]
	SET 
		[UpdateDate] = [inserted].[UpdateDate],
		[UpdateTime] = [UpdateTime] + 1
	FROM 
		[inserted],[PatientRecord]
	WHERE
		[PatientRecord].[Id] =  [inserted].[PRId] 	
GO

--endregion